<?php


namespace app\lmsx\admin;

use app\common\builder\ZBuilder;
use app\lmsx\model\Maps as MapsModel;
use think\Db;

/**
 * 一键导航
 * @author lan
 */
class Maps extends Base
{
    public function index(){
    // 获取查询和排序规则
    $map = $this->getMap();
    $order = $this->getOrder();
    $data = Db::table('dp_lmsx_maps')
    ->where($map)
        ->order($order)
        ->order('id desc')
        ->paginate()->each(function($item){
            //dump($item);die;
            return $item;
        });
    return ZBuilder::make('table')
        ->setTableName('lmsx_maps')  //需要修改 这里修改正确可以直接修改字段 删除
        ->addColumns([
            ['id','id'],
            ['name','学校名称'],
            ['position','城市'],
            ['address','学校地址'],
            //['open_time','营业时间'],
            ['hot_line','报名热线'],
            ['sort','排序'],
            ['create_time','创建时间','datetime'],
            ['right_button', '操作', 'btn']
        ])
        ->addTopButton('add', [],[])
        //->addRightButton('detail', ['icon' => 'fa fa-fw fa-eye', 'title' => '查看详情', 'href' => url('detail', ['id' => '__id__'])])
        ->addRightButton('edit',[],[])
        ->addTopButtons(['delete'])
        ->setRowList($data)
        ->addFilter('title')
        ->setSearch(['id' => 'ID', 'title' => '标题'])
        ->fetch();
}
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            $data['city']=$data['map'];
            $data['position']=$data['map_address'];
            // 第二部 验证数据
            $result = $this->validate($data,'Maps');
            if($result!==true) $this->error($result);
            //$data['open_time']=$data['open_time1'].'-'.$data['open_time2'];
            //dump($data);die;
            // 第三部 存储
            MapsModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }

        return ZBuilder::make('form')
            ->addFormItems([
                ['text','name','学校名称','必须'],
                ['bmap','map','城市','LDW8VplRGbRP6RDP8KTtqdK9zfeQduEn'],
                ['textarea','address','学校地址','必须'],
//                ['time','open_time1','营业开始时间','必须，点击选择','','HH:mm'],
//                ['time','open_time2','营业结束时间','必须，点击选择','','HH:mm'],
                ['text','hot_line','报名热线','必须'],
                ['number','sort','排序','数字越大越靠前'],
            ])
           // ->addBmap('map', '城市', 'LDW8VplRGbRP6RDP8KTtqdK9zfeQduEn', '', '116.382517,39.917259')
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $data['city']=$data['map'];
            $data['position']=$data['map_address'];
//            $data['open_time']=$data['open_time1'].'-'.$data['open_time2'];
            //验证数据
            $result = $this->validate($data,'Maps');
            if($result!==true) $this->error($result);
            MapsModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
       // $data = ArticleModel::get($id);
        $data=Db::table('dp_lmsx_maps')->where('id',$id)->find();
//        $dm=explode("-",$data['open_time']);
//        $data['open_time1']=$dm[0];
//        $data['open_time2']=$dm[1];
        $data['map']=$data['city'];
        $data['map_address']=$data['position'];
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],
                ['text','name','学校名称','必须'],
                ['bmap','map','城市','LDW8VplRGbRP6RDP8KTtqdK9zfeQduEn'],
                ['textarea','address','学校地址','必须'],
//                ['time','open_time1','营业开始时间','必须选择','','HH:mm'],
//                ['time','open_time2','营业结束时间','必须，点击选择','','HH:mm'],
                ['text','hot_line','报名热线','必须'],
                ['number','sort','排序','数字越大越靠前'],
            ])
            ->setFormData($data)
            ->fetch();
    }

    /**
     * 查看详情
     */
//    public function detail($id=null){
//        if (empty($id)) $this->error('参数错误');
//        $info=Db::table('dp_lmsx_article')->where('id',$id)->find();
//        // 使用ZBuilder快速创建表单
//        return ZBuilder::make('form')
//            ->setPageTitle('编辑') // 设置页面标题
//            ->addFormItems([ // 批量添加表单项
//                ['hidden', 'id'],
//                ['static', 'title', '标题'],
//                ['static', 'type_id', '文章分类','',CategoryModel::getArticleList()],
//                ['ueditor', 'content', '文章内容'],
//                ['static', 'view', '浏览量']
//            ])
//            ->hideBtn('submit')
//            ->setFormData($info) // 设置表单数据
//            ->fetch();
//    }


}